import 'dart:io';

import 'package:music_player/model/musci_model.dart';
import 'package:music_player/model/theme_model.dart';
import 'package:music_player/utils/time_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:palette_generator/palette_generator.dart';
import 'package:provider/provider.dart';

class MusicCore extends StatelessWidget {
  const MusicCore({super.key});

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Expanded(flex: 1, child: MusicImage()),
        Expanded(flex: 1, child: MusicWords())
      ],
    );
  }
}

class MusicImage extends StatefulWidget {
  const MusicImage({super.key});

  @override
  State<MusicImage> createState() => _MusicImageState();
}

class _MusicImageState extends State<MusicImage> {
  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints) {
      final musicModel = context.watch<MusicModel>();

      final themeModel = context.read<ThemeModel>();
      if(themeModel.imageTheme) {
        PaletteGenerator.fromImageProvider(
          NetworkImage(musicModel.thisMusic.imageUrl),
          // 也可以使用 AssetImage 或其他 ImageProvider
          maximumColorCount: 2, // 提取最多20种颜色
        ).then((PaletteGenerator paletteGenerator) {
          themeModel.bgColor = paletteGenerator.colors.toList();
        }).catchError((err) {
          themeModel.bgColor = [Colors.black, Colors.black];
        });
      }

      final size = constraints.maxHeight > constraints.maxWidth
          ? constraints.maxWidth
          : constraints.maxHeight;

      // 动态使用封面
      final ImageProvider image;
      if (musicModel.thisMusic.imageUrl.startsWith("http")) {
        image = NetworkImage(musicModel.thisMusic.imageUrl);
      } else {
        image = FileImage(File(musicModel.thisMusic.imageUrl));
      }

      return Center(
        child: Image(
          image: image,
          fit: BoxFit.cover,
          height: size * 2 / 5,
          width: size * 2 / 5,
          errorBuilder:
              (BuildContext context, Object exception, StackTrace? stackTrace) {
            return Image.asset(
              "/images/img.png",
              fit: BoxFit.cover,
              height: size * 2 / 5,
              width: size * 2 / 5,
            );
          },
        ),
      );
    });
  }
}

class MusicWords extends StatefulWidget {
  const MusicWords({super.key});

  @override
  State<MusicWords> createState() => _MusicWordsState();
}

class _MusicWordsState extends State<MusicWords> {
  // List<Word> _words = [];

  @override
  void initState() {
    super.initState();
    /*for (int i = 0; i < 10; i++) {
      _words.add(const Word("hellohellohellohellohellohellohel"));
    }*/
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints) {
      final musicModel = context.watch<MusicModel>();
      final size = constraints.maxWidth * 1 / 5;
      return Container(
          margin: EdgeInsets.only(top: size, left: 20, bottom: 30, right: size),
          child: musicModel.thisMusic.showLrc
              ? ListView.builder(
                  controller: musicModel.scrollController,
                  itemCount: musicModel.lrcList.length,
                  itemBuilder: (BuildContext context, int index) {
                    /*if (index == 0) {
                    final musicModel = context.watch<MusicModel>();
                    print("hello wrod title");
                    _words[0] =
                        Word(musicModel.thisMusic.playUrl, title: true,);
                  }*/
                    return musicModel.lrcList[index];
                  })
              : Column(
                  children: [
                    Word(
                      musicModel.thisMusic.title,
                      title: true,
                      played: true,
                    ),
                    const Expanded(
                        child: Center(
                      child: Word("暂无歌词!"),
                    ))
                  ],
                ));
    });
  }
}

class Word extends StatefulWidget {
  final bool played;
  final String text;
  final bool title;

  const Word(this.text, {this.played = false, this.title = false, super.key});

  @override
  State<Word> createState() => _WordState();
}

class _WordState extends State<Word> {
  bool hover = false;

  @override
  Widget build(BuildContext context) {
    final musicModel = context.watch<MusicModel>();
    final size = widget.title ? 30.0 : 20.0;
    return LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints) {
      return Container(
        margin: const EdgeInsets.only(top: 15),
        child: InkWell(
          onTap: () {
            print("hello_${widget.text}");
            // musicModel.scrollController.animateTo(500.0, duration: Duration(seconds: 1), curve: Curves.linear);
          },
          onHover: (it) {
            setState(() {
              hover = it;
            });
          },
          child: Text(
            widget.text,
            style: TextStyle(
              backgroundColor: hover ? const Color(0x0fffffff) : null,
              color: widget.played ? Colors.white : Colors.grey[500],
              fontSize: size,
            ),
          ),
        ),
      );
    });
  }
}
